Returns a Lehmer matrix, where
\[ A_{ij} = \begin{cases} i/j, & \text{if }j\geq{i} \\ j/i, & \text{if }j<i \end{cases} \]
lehmer <- function(n = "number of columns and rows, nXn matrix",
roundTo = "rounding of matrix if desired") {
theVector <- rep(NA, n*n)
theMatrix <- matrix(theVector, nrow = n, ncol = n)
for(j in 1:n) {
for(i in 1:n) {
if (j >= i) {
theMatrix[i,j] <- i/j
}
else
theMatrix[i,j] <- j/i
}
}
return(theMatrix)
}
lehmer(n = 6)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667
## [2,] 0.5000000 1.0000000 0.6666667 0.5000000 0.4000000 0.3333333
## [3,] 0.3333333 0.6666667 1.0000000 0.7500000 0.6000000 0.5000000
## [4,] 0.2500000 0.5000000 0.7500000 1.0000000 0.8000000 0.6666667
## [5,] 0.2000000 0.4000000 0.6000000 0.8000000 1.0000000 0.8333333
## [6,] 0.1666667 0.3333333 0.5000000 0.6666667 0.8333333 1.0000000
Test whether A is symmetric or not.
A <- lehmer(100)
all.equal(t(A), A)
## [1] TRUE
Yes, A is a symmetric matrix.
Calculate the inverse of A and assign it to ‘C’. Test whether the inverse is correct.
A <- lehmer(10)
C <- solve(A)
round(C %*% A, 3)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 0 0 0 0 0 0 0 0 0
## [2,] 0 1 0 0 0 0 0 0 0 0
## [3,] 0 0 1 0 0 0 0 0 0 0
## [4,] 0 0 0 1 0 0 0 0 0 0
## [5,] 0 0 0 0 1 0 0 0 0 0
## [6,] 0 0 0 0 0 1 0 0 0 0
## [7,] 0 0 0 0 0 0 1 0 0 0
## [8,] 0 0 0 0 0 0 0 1 0 0
## [9,] 0 0 0 0 0 0 0 0 1 0
## [10,] 0 0 0 0 0 0 0 0 0 1
Yes, \(C*A\) is the identity matrix.
Show that AxB does not always equal BxA.
A <- matrix(c(3, 1, 3, -1, 0, -2, 2, 3, -5), nrow = 3)
B <- matrix(c(3, 7, -1, -6, -14, 2, -3, -7, 1), nrow = 3)
# A*B
A %*% B
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
# B*A
B%*%A
## [,1] [,2] [,3]
## [1,] -6 3 3
## [2,] -14 7 7
## [3,] 2 -1 -1
A bank makes four kinds of loans to its personal customers, and these loans yield the following annual interest rates to the Bank:
We are interested in the bank’s lending strategy. The information we know is as following:
Calculate the lending strategy using matrix inversion.
A <- matrix(c(1,1,1,1,.14,.2,.2,.1,1,0,0,0,0,1,0,0), nrow = 4, byrow = TRUE)
b <- c(250000000, 37500000, 76388889.7, 62500000)
loan_amounts <- solve(A)%*%b
interest_rates <- c(.14, .2, .2, .1)
A
## [,1] [,2] [,3] [,4]
## [1,] 1.00 1.0 1.0 1.0
## [2,] 0.14 0.2 0.2 0.1
## [3,] 1.00 0.0 0.0 0.0
## [4,] 0.00 1.0 0.0 0.0
b
## [1] 250000000 37500000 76388890 62500000
loan_amounts
## [,1]
## [1,] 76388890
## [2,] 62500000
## [3,] 31944444
## [4,] 79166666